package ipdesk.model.report.seminar.action;

import java.util.List;
import java.util.Map;

import ipdesk.model.report.seminar.bean.SeminarInfoBean;
import ipdesk.model.report.seminar.dao.SeminarInfoDao;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import ecg.util.FileService;
import ecg.util.PageUtil;
import ecg.util.Time;

public class SeminarInfoSelectAction extends ActionSupport implements Preparable, ModelDriven<SeminarInfoBean>
{
	private SeminarInfoBean seminarInfoBean;
	private Map pageInfoDataMap;
	private List<SeminarInfoBean> seminarList;
	private String deskCode;
	private int totalCount;
	private SeminarInfoBean seminarInfoDetail;
	private SeminarInfoBean fileDetail;
	
	FileService fileService = new FileService();

	public String execute() throws Exception
	{
		
		totalCount = SeminarInfoDao.getInstance().getDataTotalCount(seminarInfoBean);
		
	
		pageInfoDataMap = PageUtil.getInstance().getPageInfoData(totalCount, seminarInfoBean.getPageNumber());
		
		if(seminarInfoBean.getDeskCode() != null || !"".equals(seminarInfoBean.getDeskCode())){
			pageInfoDataMap.put("deskCode", seminarInfoBean.getDeskCode());
			deskCode = seminarInfoBean.getDeskCode();
		}
		
		if(seminarInfoBean.getOrder() != null || !"".equals(seminarInfoBean.getOrder())){
			pageInfoDataMap.put("order", seminarInfoBean.getOrder());
		}
		
		if(seminarInfoBean.getYear() != null || !"".equals(seminarInfoBean.getYear())){
			pageInfoDataMap.put("year", seminarInfoBean.getYear());
		}
		
		seminarList = SeminarInfoDao.getInstance().getPagingDataListForUser(pageInfoDataMap); 
		
		System.out.println("size : : "+ seminarList.size());
		
		return SUCCESS;
	}
	
	public String executeDetail() throws Exception
	{
		int seminarSeq = seminarInfoBean.getSeminarSeq();
		System.out.println("seminarSeq :: "+seminarSeq);
		
		seminarInfoDetail = SeminarInfoDao.getInstance().getDetailData(seminarSeq);
		
		fileDetail = SeminarInfoDao.getInstance().getFileDetailData(seminarSeq, "FLE03");
		
		if(fileDetail != null) {
			seminarInfoDetail.setPathName(fileDetail.getPathName());
			seminarInfoDetail.setFileName(fileDetail.getFileName());
		}
		
		fileDetail = SeminarInfoDao.getInstance().getFileDetailData(seminarSeq, "FLE02");
		
		if(fileDetail != null) {
			seminarInfoDetail.setPathName(fileDetail.getPathName());
			seminarInfoDetail.setPicName1(fileDetail.getFileName());
		}
		
		seminarInfoDetail.setServerName(getText("path.upload.prefix"));
		
		return SUCCESS;
	}

	
	public String executeModifyForm() throws Exception
	{
		int seminarSeq = seminarInfoBean.getSeminarSeq();
		System.out.println("seminarSeq :: "+seminarSeq);
		
		seminarInfoDetail = SeminarInfoDao.getInstance().getDetailData(seminarSeq);
		
		fileDetail = SeminarInfoDao.getInstance().getFileDetailData(seminarSeq, "FLE03");
		
		if(fileDetail != null) {
			seminarInfoDetail.setPathName(fileDetail.getPathName());
			seminarInfoDetail.setFileName(fileDetail.getFileName());
			seminarInfoDetail.setFileSeq1(fileDetail.getFileSeq());
		}
		
		fileDetail = SeminarInfoDao.getInstance().getFileDetailData(seminarSeq, "FLE02");
		
		if(fileDetail != null) {
			seminarInfoDetail.setPathName(fileDetail.getPathName());
			seminarInfoDetail.setPicName1(fileDetail.getFileName());
			seminarInfoDetail.setFileSeq2(fileDetail.getFileSeq());
		}
		
		System.out.println("***fileSeq1 : " + seminarInfoDetail.getFileSeq1());
		System.out.println("***fileSeq2 : " + seminarInfoDetail.getFileSeq2());
		
		return SUCCESS;
	}
	
	
	public String executeDelete() throws Exception
	{
		int seminarSeq = seminarInfoBean.getSeminarSeq();
		System.out.println("seminarSeq :: "+seminarSeq);
		
		if(seminarInfoBean.getFileSeq() != null || !"".equals(seminarInfoBean.getFileSeq())){
			String fileSeq = seminarInfoBean.getFileSeq();
			
			// 1. 파일이 있다면, 파일 먼저 삭제
			String filePathPrefix = getText("path.upload.prefix");
			String fileName = seminarInfoBean.getUploadFileName();
			String pathName = seminarInfoBean.getPathName();
			String picName1 	= seminarInfoBean.getPicName1();
			
			fileService.deleteFile(filePathPrefix + pathName + fileName);
			fileService.deleteFile(filePathPrefix + pathName + picName1);
			
			// 2. file db에서 데이터 삭제
			SeminarInfoDao.getInstance().delFileData(fileSeq);
		}
		// 2. 글 삭제
		SeminarInfoDao.getInstance().delDetailData(seminarSeq);	
		
		return SUCCESS;
	}
	
	public String executeDeleteFile() throws Exception
	{		
		String fileName = "";
		

		System.out.println("*s**fileSeq2 : " + seminarInfoBean.getFileSeq2());
			
		if(seminarInfoBean.getFileSeq1() != null) {
			seminarInfoBean.setFileSeq(seminarInfoBean.getFileSeq1());
			fileName = seminarInfoBean.getFileName();
		}
			
		if(seminarInfoBean.getFileSeq2() != null) {
			seminarInfoBean.setFileSeq(seminarInfoBean.getFileSeq2());
			fileName = seminarInfoBean.getPicName1();
		}
			
		if(seminarInfoBean.getFileSeq() != null || !"".equals(seminarInfoBean.getFileSeq())) {
			String fileSeq = seminarInfoBean.getFileSeq();
				
			// 파일 있으면 파일부터 삭제
			String filePathPrefix = getText("path.upload.prefix");
			String pathName = seminarInfoBean.getPathName();
			System.out.println("***경로 : "+pathName);
			fileService.deleteFile(filePathPrefix + pathName + fileName);
				
			// 디비 파일삭제
			SeminarInfoDao.getInstance().delFileData(fileSeq);
		}
		
		return SUCCESS;
	}
	
	public String executeModify() throws Exception
	{		
		//seminarInfoDetail = SeminarInfoDao.getInstance().getDetailData(seminarInfoBean.getSeminarSeq());
		
		// 데이터 업데이트, 파일 업로드 처리.
		String uplodaPathPrefix = getText("path.upload.prefix");
		String basePath = getText("path.upload.seminar");
		String totalPath = uplodaPathPrefix + basePath;
		
		seminarInfoBean.setPathName(basePath);
		
		String regTime = Time.getDateTime();
		seminarInfoBean.setRegTime(regTime);
		
		SeminarInfoDao.getInstance().seminarInfoUpdate(seminarInfoBean);
		
		if(seminarInfoBean.getUpload() != null) {
			String uploadFileName = seminarInfoBean.getUploadFileName();
			String realFileName = fileService.saveFile(seminarInfoBean.getUpload(), totalPath, uploadFileName);
			seminarInfoBean.setUploadFileName(realFileName);
			seminarInfoBean.setFileCategory("FLE03");
			
			SeminarInfoDao.getInstance().insertFileData(seminarInfoBean);
		}
		
		if(seminarInfoBean.getUploadPic() != null) {
			String uploadPicName = seminarInfoBean.getUploadPicFileName();
			String realFileName = fileService.saveFile(seminarInfoBean.getUploadPic(), totalPath, uploadPicName);
			seminarInfoBean.setUploadFileName(realFileName);
			seminarInfoBean.setFileCategory("FLE02");
			
			SeminarInfoDao.getInstance().insertFileData(seminarInfoBean);
		}
		
		return SUCCESS;
	}
	
	public SeminarInfoBean getSeminarInfoDetail() {
		return seminarInfoDetail;
	}

	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		seminarInfoBean = new SeminarInfoBean();
	}

	public SeminarInfoBean getModel() {
		// TODO Auto-generated method stub
		return seminarInfoBean;
	}

	public List<SeminarInfoBean> getSeminarList() {
		return seminarList;
	}

	public void setSeminarList(List<SeminarInfoBean> seminarList) {
		this.seminarList = seminarList;
	}
	
	public Map getPageInfoDataMap()
	{
		return pageInfoDataMap;
	}

	public void setPageInfoDataMap(Map pageInfoDataMap)
	{
		this.pageInfoDataMap = pageInfoDataMap;
	}
	
	public String getDeskCode() {
		return deskCode;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public SeminarInfoBean getFileDetail() {
		return fileDetail;
	}
	
}

